<?php

class schoolManagerUsersTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));

    $this->namespace        = 'managers';
    $this->name             = 'createUsers';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [schoolManagerUsers|INFO] task does things.
Call it with:

  [php symfony schoolManagerUsers|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    // add your code here
    echo 'Synching manager users...'.chr(10);
    
    $q = TeacherTable::getInstance()->getSchoolManagersQuery();
    foreach($q->fetchArray() as $teacher_row){
        if($teacher_row['is_manager']){
            $manager_user = sfGuardUserTable::getInstance()->getSchoolManagerUser($teacher_row, true);
            echo 'User created for teacher '.$teacher_row['f_name'].' '.$teacher_row['l_name'].', username:'.$manager_user->getUsername().chr(10); 
        }
    }
    
    
    $q = TeacherTable::getInstance()->getTrainersQuery();
    foreach($q->fetchArray() as $teacher_row){
        if($teacher_row['is_trainer']){
            $trainer_obj = TrainerTable::getInstance()->getOrCreateTrainer($teacher_row);
            $trainer_user = sfGuardUserTable::getInstance()->getTrainerUser($teacher_row, true);
            echo 'User created for Trainer '.$teacher_row['f_name'].' '.$teacher_row['l_name'].', username:'.$trainer_user->getUsername().chr(10);             
        }
    }
  }
}
